package carlyang;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class PrintStreamTest {
	public static Path debugPath = Paths.get("E:/adt_workplace/log/printWriter.txt");
	public static Charset charsetChineseSimple = Charset.forName("GB2312");
	public static Charset charsetDef = Charset.defaultCharset();//默认GBK
	public static Charset charsetUsascii = Charset.forName("US-ASCII");//ascii
	public static SimpleDateFormat dateF = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
	private OutputStream outputStream = null;
	private BufferedWriter bufferedWriter = null;
	private PrintWriter printWriter = null;

//	public PrintStreamTest() {
//		// TODO Auto-generated constructor stub
//	}

	public PrintStreamTest(Path path,Charset charset) {
		// TODO Auto-generated constructor stub
		try {
//			outputStream = Files.newOutputStream(path, StandardOpenOption.CREATE,
//					StandardOpenOption.APPEND);
			Path path2 = Paths.get("E:/adt_workplace/log/"+dateF.format(new Date())+".txt");
			if (!Files.exists(path2.getParent())){
				Files.createDirectory(path2.getParent());
			}
			
			bufferedWriter = Files.newBufferedWriter(path2, charset,StandardOpenOption.CREATE,StandardOpenOption.APPEND);
			printWriter = new PrintWriter(bufferedWriter);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void write(String string) {
		printWriter.println(string);
		
	}
	public void close() {
		if(outputStream !=null)
			try {
				outputStream.close();
				outputStream = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(bufferedWriter != null)
			try {
				bufferedWriter.close();
				bufferedWriter = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(printWriter != null){
			printWriter.close();
			printWriter = null;
		}
		
	}
	public static void main(String[] args) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		PrintStreamTest printStreamTest = new PrintStreamTest(debugPath, charsetUsascii);
		printStreamTest.write("time:"+dateFormat.format(new Date()));
		printStreamTest.close();
	}
}
